package com.kjtpay.design_pattern.memento;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class MementoActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, MementoActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    GameRole gameRole;
    RoleStateCaretaker caretaker;
    TextView stateTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memento);
        setTitle("备忘录模式");

        stateTextView = findViewById(R.id.textViewState);
        gameRole = new GameRole();
        caretaker = new RoleStateCaretaker();

        // 初始状态是满血复活的状态
        gameRole.initState();
        stateTextView.setText(gameRole.display());
    }

    public void onInitialButtonClick(View view) {
        gameRole.initState();
        stateTextView.setText(gameRole.display());
    }

    public void onSaveButtonClick(View view) {
        caretaker.setMemento(gameRole.saveState());
    }

    public void onRecoveryButtonClick(View view) {
        gameRole.recoveryState(caretaker.getMemento());
        stateTextView.setText(gameRole.display());
    }

    public void onFightButtonClick(View view) {
        gameRole.fight();
        stateTextView.setText(gameRole.display());
    }
}